package com.mall.b2c.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.b2c.enums.DetailType;
import com.mall.b2c.service.PaymentDetailService;
import com.mall.b2c.util.JsonDataGridHelper;
import com.mall.common.bean.Order;
import com.mall.common.bean.Pager;
import com.mall.common.bean.SearchFilter;
import com.mall.common.bean.SearchFilter.Operator;
import com.mall.common.controller.BaseController;
import com.mall.common.util.EnumUtils;
import com.mall.common.web.Servlets;

/**
 * 后台Action类 - 收支明细
 */
@Controller
@RequestMapping("/admin/b2c/payment")
public class PaymentDetailController extends BaseController {
	@Resource private PaymentDetailService paymentDetailService;

	/**
	 * 跳转到列表页面
	 * 
	 * @return
	 */
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("detailTypes", EnumUtils.enumProp2I18nList(DetailType.class));
		return "/admin/b2c/payment/list";
	}

	/**
	 * 收支明细查询
	 * 
	 * @return
	 */
	@RequestMapping(value="/queryData", method=RequestMethod.POST)
	@ResponseBody
	public Pager queryData(Long memberId, Pager pager, HttpServletRequest request){
		Map<String, Object> filterParams = Servlets.getParametersStartingWith(request);
		List<SearchFilter> filters = SearchFilter.parse(paymentDetailService.integrationParameter(filterParams));
		pager.addOrder(Order.desc("createDate"));
		pager.addOrder(Order.desc("id"));
		if(memberId != null){
			pager.addSearchFilter(new SearchFilter("member.id", Operator.EQ, memberId));
			pager = paymentDetailService.findPage(pager);
		}else{
			pager = paymentDetailService.findPage(pager, filters);
		}
		return JsonDataGridHelper.createJSONData(pager);
	}
	
}
